package starter.pageobjects.orangehrmlive;

/**
 * @ClassName LoginPage
 * @Date 2024/10/28 16:14 16
 * @Author Jack Chen
 * @Description serenity-cucumber-page-objects-starter-master
 * @Version 1.0
 */
import net.serenitybdd.annotations.DefaultUrl;
import net.serenitybdd.core.pages.PageObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

@DefaultUrl("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login")
public class LoginPage extends PageObject {
    public static final By USERNAME = By.xpath("//input[@name='username']");
    public static final By PASSWORD = By.xpath("//input[@name='password']");
    public static final By LOGIN = By.xpath("//button[@type='submit']");
    public static final By LOGIN_TITTLE = By.cssSelector(".orangehrm-login-title");
    private static final By USERNAMEREQUIREMSG = By.xpath("//div[@class='oxd-form-row'][1]//span");
    private static final By USERPASSREQUIREMSG = By.xpath("//div[@class='oxd-form-row'][2]//span");
    private static final By INVALIDCREDENTIALS = By.cssSelector(".orangehrm-login-error i+p.oxd-text");

    public void usernameInput(String username) {
        find(LoginPage.USERNAME).type(username);
    }

    public void passwordInput(String password) {
        find(LoginPage.PASSWORD).type(password);
    }

    public void clickOnLogon() {
        find(LoginPage.LOGIN).click();
    }

    public String getLoginTitle() {
        Wait<WebDriver> wait = new WebDriverWait(getDriver(), Duration.ofSeconds(3));
        WebElement titleEle = wait.until(ExpectedConditions.visibilityOfElementLocated(LoginPage.LOGIN_TITTLE));
        return titleEle.getText();
    }

    public String getNameReqWarnMsg() {
        Wait<WebDriver> wait = new WebDriverWait(getDriver(), Duration.ofSeconds(3));
        WebElement nameReqMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(LoginPage.USERNAMEREQUIREMSG));
        return nameReqMsg.getText();
    }

    public String getPassReqWarnMsg() {
        Wait<WebDriver> wait = new WebDriverWait(getDriver(), Duration.ofSeconds(3));
        WebElement nameReqMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(LoginPage.USERPASSREQUIREMSG));
        return nameReqMsg.getText();
    }

    public String getInvalidCredentialsErrMsg() {
        Wait<WebDriver> wait = new WebDriverWait(getDriver(), Duration.ofSeconds(3));
        WebElement nameReqMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(LoginPage.INVALIDCREDENTIALS));
        return nameReqMsg.getText();
    }
}
